package com.elastic.controller;

import com.jfinal.core.Controller;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * @author: 李德才
 * @description:
 * @create: 2021-04-13 20:55
 **/
public class IndexController extends Controller {


    public void bulk() throws IOException {
        RestHighLevelClient client = ESClient.getInstance().getHighLevelClient();
        String CAR_INDEX = "car_index";
        BulkRequest request = new BulkRequest(CAR_INDEX);
//        request.add(new DeleteRequest(CAR_INDEX, "13"));
        request.add(new UpdateRequest(CAR_INDEX, "22")
                .doc(XContentType.JSON, "series_name", "天籁之音"));
//        IndexRequest: 没有就创建，有就覆盖
        request.add(new IndexRequest(CAR_INDEX).id("4")
                .source(XContentType.JSON, "brand_name", "天津一汽"));
        BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
        renderJson(bulkResponse.getItems());
    }
}
